package com.sunyard.manage.controller;

import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.bind.annotation.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.servlet.http.HttpServletRequest;

/**
 * @author TSZ
 * @Description:
 * @date:2022-10-18 09:55
 */
@Api(tags = "ZhongHangController", description = "江苏中行校验ticket")
@Slf4j
@RestController
@RequestMapping(value = "/ticket", method = RequestMethod.POST)
public class ZhongHangController {

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("校验ticket")
    public BaseResult getTerminalByPage(@RequestBody String ticket, HttpServletRequest request) {

        return GetResult.getResultSuccess("登录成功");
    }

    public String getXml(String name)
    {
        String result = null;
        try {

            String endpoint = "http://22.200.1.93/bocpws/bocpcommon.asmx";
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.addParameter(new javax.xml.namespace.QName(
                            "http://tempuri.org/", "ticket"),
                    org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);
            String sa = "http://tempuri.org/user_ticket";
            call.setOperationName(new javax.xml.namespace.QName(
                    "http://tempuri.org/", "user_ticket"));
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(sa);
            result = (String) call.invoke(new Object[] { name});

        } catch (Exception e) {

            System.err.println(e.toString());
            e.printStackTrace();


        }
        return result;
    }

    public static String readUserId(String xml){
        Document doc = null;

        try {
            doc = DocumentHelper.parseText(xml);
            Element rootElt = doc.getRootElement();
            Element state = rootElt.element("state");
            if ("0".equals(state.getTextTrim())){
                String sid = rootElt.element("sid").getTextTrim();
                String uid = rootElt.element("uid").getTextTrim();
                String expireDate = rootElt.element("expire_date").getTextTrim();

                System.out.println("sid:" + sid);
                System.out.println("sid:" + uid);
                System.out.println("sid:" + expireDate);
            } else {
                log.error("获取ticket用户状态失败,ticket内容:{}",xml);
            }
        }catch (Exception e){
            log.error("获取ticket用户状态失败,ticket内容:{}",xml);
        }
        return null;
    }
}